package com.msmall.community.tag;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.msmall.community.ApplicationManager;
import com.msmall.community.team.model.Team;
import com.msmall.community.team.model.TeamProperty;
import com.msmall.community.team.service.TeamManager;

public class TeamPropertyTag extends TagSupport{
	
	private static final long serialVersionUID = 5519842521452264132L;

	private final Log log = LogFactory.getLog(TeamPropertyTag.class);
	
	private boolean admin;//是否是后台查看
	
	private Long userid;//查看人ID
	
	private Long teamid;
	
	private String propertyKey;
	
	public int doStartTag() throws JspException {
		return (SKIP_BODY);
	}

	public int doEndTag() throws JspException {
		TeamManager manager = (TeamManager) ApplicationManager.appContext.getBean("teamManager");
		JspWriter writer = pageContext.getOut();
		HttpServletRequest req = (HttpServletRequest) super.pageContext.getRequest();
		HttpSession session = req.getSession();
		String info = "";
		try{
			boolean canSee = false;
			TeamProperty property = null;
			String value = "";
			if(admin)
				canSee = true;
			if(canSee){
				if(TeamProperty.KEY_VISIT.equalsIgnoreCase(propertyKey)){
					property = manager.getTeamVisit(teamid);
					if(property != null){
						value = property.getPropertyValue();
						if(TeamProperty.VALUE_DEFAULT_ALL.equals(value)){
							info = "所有人可见";
						}else if(TeamProperty.VALUE_DEFAULT_ONLY_MEMBER.equals(value)){
							info = "只有组成员可见";
						}
					}
				}else if(TeamProperty.KEY_SHARED_TEAM.equalsIgnoreCase(propertyKey)){
					property = manager.getSharedTeam(teamid);
					if(property != null){
						value = property.getPropertyValue();
						if(value != null){
							String[] ids = value.split(",");
							for(int i=0; i<ids.length; i++){
								try{
								Long tid = Long.valueOf(ids[i].trim());
								Team team = (Team) manager.get(Team.class, tid);
								if(team != null)
									info = info + (team.getName() + ",");
								}catch(Exception e){
									continue;
								}
							}
						}
					}
				}else if(TeamProperty.KEY_AUTHRIZED_USER.equalsIgnoreCase(propertyKey)){
				}else if(TeamProperty.KEY_APPLY_AUDIT.equalsIgnoreCase(propertyKey)){
					
				}else{
					
				}
			}else{
				
			}
		}catch(Exception e){
			log.error(e);
		}
		try {
			writer.print(info.toString());
		} catch (IOException e) {
			log.error(e);
		}
		return (SKIP_BODY);
	}

	public Long getUserid() {
		return userid;
	}

	public void setUserid(Long userid) {
		this.userid = userid;
	}

	public Long getTeamid() {
		return teamid;
	}

	public void setTeamid(Long teamid) {
		this.teamid = teamid;
	}
}
